package cn.wolfcode.crm.config;

import cn.wolfcode.crm.shiro.CrmRealm;
import cn.wolfcode.crm.util.MyFreeMarkerConfig;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    /**
     *  freeMarker 提供 Shiro 标签
     * @return
     */
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(){
        MyFreeMarkerConfig myFreeMarkerConfig = new MyFreeMarkerConfig();
        myFreeMarkerConfig.setTemplateLoaderPath("classpath:templates/");
        return myFreeMarkerConfig;

    }

    /**
     * 配置缓存管理器
     * @return
     */
    @Bean
    protected CacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }

    /**
     * 配置安全管理器
     * 关联自定义的数据源
     * @param crmRealm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager(CrmRealm crmRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置为自定义的 crmRealm 数据源
        securityManager.setRealm(crmRealm);
        // 设置缓存管理器
        securityManager.setCacheManager(cacheManager());
        return securityManager;
    }

    /**
     * 配置 Shiro 过滤器
     * 引用 securityManager 安全管理器
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        // 引用安全管理器
        shiroFilter.setSecurityManager(securityManager);
        // 过滤路径,拦截所有的请求,不登录不允许访问
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/logout.do", "logout");
        map.put("/css/**", "anon");
        map.put("/js/**", "anon");
        map.put("/loginUser.do", "anon");
        map.put("/**", "authc");
        // 不拦截的请求,登录请求,设置默认跳转到 login.html
        shiroFilter.setLoginUrl("/login.html");
        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }

    /**
     * 开启 shrio 注解扫描器
     * @param crmRealm
     * @return
     */
/*   @Bean
   public AuthorizationAttributeSourceAdvisor advisor(CrmRealm crmRealm){
       AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
       // 设置安全管理器
       advisor.setSecurityManager(securityManager(crmRealm));
       return advisor;
   }*/

    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

}
